package com.hotel.front;

import java.io.IOException;
import java.util.List;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.appengine.api.datastore.EntityNotFoundException;
import com.google.gson.Gson;
import com.hotel.BL.ReasonCancelLogic;


import data.ReasonCancel;

@SuppressWarnings("serial")
public class ReasonCancelService extends HttpServlet{
	//http://localhost:8080/reasonCancelService?func=addReasonCancel&reason=1&description=aa
		//http://localhost:8080/reasonCancelService?func=editReasonCancel&reasonId=874&reason=12345&description=aabbb
		//http://localhost:8080/reasonCancelService?func=delReasonCancel&reasonId=874
		//http://localhost:8080/reasonCancelService?func=loadReasonCancel
	public void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws IOException {
		try{
			String doClass = req.getParameter("func");
			if(doClass.equals("addReasonCancel")){
				AddReasonCancel(req,resp);
			}else if(doClass.equals("editReasonCancel")){
				EditReasonCancel(req,resp);
			}else if(doClass.equals("delReasonCancel")){
				DeleteReasonCancel(req,resp);
			}else if(doClass.equals("loadReasonCancel")){
				LoadReasonCancel(req,resp);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	private void LoadReasonCancel(HttpServletRequest req,
			HttpServletResponse resp) throws IOException {
		// TODO Auto-generated method stub
		List<ReasonCancel> rc = new ReasonCancelLogic().GetAllReasonCancel();
		Gson gson = new Gson();
		resp.setContentType("text/plain; charset=UTF-8");
		resp.getWriter().println(gson.toJson(rc));
		
	}

	private void DeleteReasonCancel(HttpServletRequest req,
			HttpServletResponse resp) throws EntityNotFoundException, IOException {
		// TODO Auto-generated method stub
		
	long reasonId = Long.parseLong(req.getParameter("reasonId"));
		
	ReasonCancel obj = new ReasonCancelLogic().DeleteReasonCancel(reasonId);
		Gson gson = new Gson();

		resp.setContentType("text/plain; charset=UTF-8");
		resp.getWriter().println(gson.toJson(obj));
		
	}

	private void EditReasonCancel(HttpServletRequest req,
			HttpServletResponse resp) throws EntityNotFoundException, IOException {
		// TODO Auto-generated method stub
		long reasonId = Long.parseLong(req.getParameter("reasonId"));
		String reason = req.getParameter("reason");
		String description = req.getParameter("description");
		
		ReasonCancel obj = new ReasonCancelLogic().EditReasonCancel(reasonId, reason, description);
		Gson gson = new Gson();

		resp.setContentType("text/plain; charset=UTF-8");
		resp.getWriter().println(gson.toJson(obj));
		
	}

	private void AddReasonCancel(HttpServletRequest req,
			HttpServletResponse resp) throws EntityNotFoundException, IOException {
		// TODO Auto-generated method stub
		
		String reason = req.getParameter("reason");
		String description = req.getParameter("description");
		
		ReasonCancel obj = new ReasonCancelLogic().AddReasonCancel( reason, description);
		Gson gson = new Gson();

		resp.setContentType("text/plain; charset=UTF-8");
		resp.getWriter().println(gson.toJson(obj));
	}

}

